//*****************************************************************************
// JFab (http://code.google.com/p/jfab)
// Copyright (c) 2011-2012 JFab.org
// Admin jfab@jeffreythompson.net
//
// See the file "LICENSE.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//*****************************************************************************

package org.jfab.swingui;

import java.awt.BorderLayout;
import java.io.StringWriter;
import java.io.Writer;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.jfab.core.DefaultInjector;
import org.jfab.core.Injector;
import org.jfab.core.JFabber;
import org.jfab.core.model.CGClass;
import org.jfab.core.model.CGCompilationUnit;
import org.jfab.core.model.CGEnum;
import org.jfab.core.model.CGInterface;

/**
 * Provides a code pane for JFab.
 */
public class CodePanel extends JPanel
{
    /** Serial version UID. */
    private static final long serialVersionUID = 1L;

    /** Code generator compilation unit. */
    private CGCompilationUnit _cgCompilationUnit;

    /** Text area widget. */
    private final JTextArea _textArea = createTextArea();

    /**
     * Construct this object.
     */
    public CodePanel()
    {
        final JScrollPane scrollPane = new JScrollPane(_textArea);

        setLayout(new BorderLayout());

        add(scrollPane, BorderLayout.CENTER);
    }

    /**
     * @return the cgCompilationUnit
     */
    public CGCompilationUnit getCgCompilationUnit()
    {
        return _cgCompilationUnit;
    }

    /**
     * Refresh.
     */
    public void refresh()
    {
        setCgCompilationUnit(_cgCompilationUnit);
    }

    /**
     * @param cgCompilationUnit the cgCompilationUnit to set
     */
    public void setCgCompilationUnit(final CGCompilationUnit cgCompilationUnit)
    {
        setTextFrom(cgCompilationUnit);

        _cgCompilationUnit = cgCompilationUnit;
    }

    /**
     * @return a new text area widget.
     */
    private JTextArea createTextArea()
    {
        final JTextArea answer = new JTextArea();

        answer.setEditable(false);

        return answer;
    }

    /**
     * @param cgCompilationUnit Code generator compilation unit.
     */
    private void setTextFrom(final CGCompilationUnit cgCompilationUnit)
    {
        if (cgCompilationUnit == null)
        {
            _textArea.setText("");
        }
        else
        {
            final boolean isChanged = !((cgCompilationUnit == _cgCompilationUnit) || ((_cgCompilationUnit != null) && _cgCompilationUnit
                    .equals(cgCompilationUnit)));

            final Injector injector = new DefaultInjector();
            final JFabber jfabber = injector.injectJFabber();
            final Writer writer = new StringWriter();

            if (cgCompilationUnit instanceof CGInterface)
            {
                jfabber.generate(writer, (CGInterface)cgCompilationUnit);
            }
            else if (cgCompilationUnit instanceof CGClass)
            {
                jfabber.generate(writer, (CGClass)cgCompilationUnit);
            }
            else if (cgCompilationUnit instanceof CGEnum)
            {
                jfabber.generate(writer, (CGEnum)cgCompilationUnit);
            }

            _textArea.setText(writer.toString());

            if (isChanged)
            {
                _textArea.setCaretPosition(0);
            }
        }
    }
}
